template <typename VertexFormat>
VertexBuffer<VertexFormat>::VertexBuffer(void) 
{
	m_NumVerts = 0;
	m_VertsUsed = 0;
	m_pVertexBuff = 0;
}

template <typename VertexFormat>
void VertexBuffer<VertexFormat>::IncreaseVerts(UINT numVerts)
{
	UINT increments = (numVerts / s_VertSizeIncrement) * s_VertSizeIncrement + s_VertSizeIncrement;
	m_NumVerts += increments;

	IDirect3DVertexBuffer9 *pOldVertexBuff = m_pVertexBuff;

	D3D9Renderer::device->CreateVertexBuffer(m_NumVerts*sizeof(VertexFormat), D3DUSAGE_WRITEONLY, 0,
        D3DPOOL_DEFAULT, &m_pVertexBuff, 0);

	if(pOldVertexBuff)
	{
		void *oldBuffer;
		pOldVertexBuff->Lock(0,	(m_NumVerts - increments)*sizeof(VertexFormat), &oldBuffer, 0);		

		void *buffer;
		m_pVertexBuff->Lock(0, (m_NumVerts - increments)*sizeof(VertexFormat), &buffer, 0);	
		memcpy(buffer, oldBuffer, (m_NumVerts - increments)*sizeof(VertexFormat));

		pOldVertexBuff->Unlock();
		m_pVertexBuff->Unlock();

		pOldVertexBuff->Release();
	}
}

template <typename VertexFormat>
void VertexBuffer<VertexFormat>::UpdateVerts(const VertexFormat* verts, UINT startVert, UINT numVerts)
{
	if(m_pVertexBuff)
	{
		VertexFormat* buffer;
		m_pVertexBuff->Lock(startVert*sizeof(VertexFormat), numVerts*sizeof(VertexFormat), (void**)&buffer, 0);
		memcpy((void*)buffer, (void*)verts, numVerts*sizeof(VertexFormat));
		m_pVertexBuff->Unlock();
	}
}

template <typename VertexFormat>
void VertexBuffer<VertexFormat>::Initialize()
{
	IncreaseVerts(1);
	CreateVertexDeclaration();
}

template <typename VertexFormat>
UINT VertexBuffer<VertexFormat>::AddVerts(const VertexFormat *verts, UINT numVerts)
{
	//  Save the location to copy to
	UINT vertLocation = m_VertsUsed;

	// Increase the total verts
	m_VertsUsed += numVerts;

	// Check if more need to be made
	if(m_VertsUsed > m_NumVerts) 
		IncreaseVerts(m_VertsUsed - m_NumVerts);

    void *buffer;
    m_pVertexBuff->Lock(vertLocation*sizeof(VertexFormat),	
        numVerts*sizeof(VertexFormat), &buffer, 0);								
	// copy data to buffer
    memcpy(buffer, verts, numVerts*sizeof(VertexFormat));
    m_pVertexBuff->Unlock();

	return vertLocation;
}